#include <string.h>
#include <stdbool.h>
#include "c_sorts.h"

void bubblesort (char ** words, int arraysiz)
{
        char* tmp;
        bool swapped;
        int i,j;
        for (i = arraysiz-1; i > 0; --i) /*El ultimo elemento va quedando en su lugar por pasada*/
        {
                swapped = false;
                for (j = 0; j < i; ++j)
                {
                        if (strcmp(words[j], words[j+1]) > 0)
                        {
                                tmp = words[j+1];
                                words[j+1] = words[j];
                                words[j] = tmp;
                                swapped = true;
                        }
                }
                if (!swapped) break;
        }
}

void insertionsortParcial(char** words, int arraysiz, int inicio, int gap)
{
        char* tmp;
        int i,j;
        for (i = inicio; i < arraysiz; i+= gap) /*Por cada elemento del subarray*/
        {
                /* Empujar hacia atras en el subarray mientras sea menor al anterior */
                for (j = i; j-gap >= 0 && strcmp(words[j],words[j-gap]) < 0; j -= gap)
                {
                        tmp = words[j];
                        words[j] = words[j-gap];
                        words[j-gap] = tmp;
                }
        }
}

void shellsort (char ** words, int arraysiz)
{       
        int i;
        unsigned int gap = 1;
        while (gap < arraysiz) gap = 3 * gap + 1; /*Knuth*/
        while (gap > 0)
        {
                gap = gap/3;
                for (i = 0; i < gap; ++i)
                        insertionsortParcial(words, arraysiz, i, gap);
        }
}


